// 解构赋值
{
    let a, b, rest
    [a, b] = [1, 2]
    console.log(a, b)
}

//  数组解构赋值
{
    let a, b, rest
    [a, b, ...rest] = [1, 2, 3, 4, 5]
    console.log(a, b, rest)
}

//  对象解构赋值
{
    let a, b
    ({ a, b } = { a: 1, b: 2 })
    console.log(a, b)
}

//  如果解构赋值配对不成功，那多出的变量为undefined，可设置默认值
{
    let a, b, c, rest
    [a, b, c = 3] = [1, 2]
    console.log(a, b, c)
}

//  变量交换
{
    let a = 1;
    let b = 2;
    [a, b] = [b, a];
    console.log(a, b)
}

//  方法返回值
{
    function f () {
        return [1, 2]
    }
    let a, b;
    [a, b] = f();
    console.log(a, b);
}

//  数组匹配模式，逗号表示跳过一个元素
{
    function f () {
        return [1, 2, 3, 4, 5]
    }
    let a, b, c;
    [a, , , b] = f()
    console.log(a, b);
}

//  函数返回值的数组长度不确定，只取第一个值
{
    function f () {
        return [1, 2, 3, 4, 5]
    }
    let a, b, c;
    [a, ...b] = f()
    console.log(a, b);
}

//  对象解构赋值
{
    let o = { p: 42, q: true }
    let { p, q } = o
    console.log(p, q)
}

//  左边a=10为默认值，右边a:3为重新赋值
{
    let { a = 10, b = 5 } = { a: 3 }
    console.log(a, b);
}

//  嵌套对象数组
{
    let metaData = {
        title: 'abc',
        test: [{
            title: 'test',
            desc: 'description'
        }]
    }
    //  es5
    var esTitle = metaData.title;
    var cnTitle = metaData.test[0].title;
    console.log(esTitle, cnTitle)

    //  es6
    let { title: esTitle, test: [{ title: cnTitle }] } = metaData;
    console.log(esTitle, cnTitle)
}